package com.leetcode.LC;

import java.util.*;

public class LC290 {
    public boolean wordPattern(String pattern, String s) {

        String[] s1 = s.split(" ");
        if (s1.length!=pattern.length()){
            return false;
        }
        Map<Character, Integer> map = new HashMap<>();
        List<String> list=new ArrayList<>();
        for (int i = 0; i < pattern.length(); i++) {
            if (map.containsKey(pattern.charAt(i))) {
                if (!s1[i].equals(s1[map.get(pattern.charAt(i))])) {
                    return false;
                }
            } else {
                map.put(pattern.charAt(i), i);
            }
        }

        for (Integer value : map.values()) {
            if (list.contains(s1[value])){
                return false;
            }else {
                list.add(s1[value]);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new LC290().wordPattern("abba", "dog dog dog dog"));
    }
}
